using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

using LCL.Caching;
using LCL.Config;

namespace LCL.Tools
{
    public partial class FrmDoc : BaseFrm
    {
        public FrmDoc()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog1 = new OpenFileDialog();
            fileDialog1.Filter = "文件类型(*.dll,*.exe,*.dll)|*.dll;*.exe;*.dll|All files (*.*)|*.*";
            fileDialog1.FilterIndex = 1;
            fileDialog1.RestoreDirectory = true;
            if (fileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtdllpath.Text = fileDialog1.FileName;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = @"F:\Github\LCLFramework\LCLDemo\WebMvcClient\WebMvcClient\Plugins";
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtcodepath.Text = dialog.SelectedPath;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.txtdllpath.Text.Length == 0||!File.Exists(this.txtdllpath.Text))
            {
                MessageBox.Show("找不到DLL文件");
                return;
            }
            if (this.txtcodepath.Text.Length == 0)
            {
                MessageBox.Show("找不到DLL源码文件");
                return;
            }
            if (this.txtcondition.Text.Length == 0)
            {
                MessageBox.Show("查询条件不能为空");
                return;
            }
            button1.Enabled = false;
            button3.Enabled = false;
            button2.Enabled = false;
            string condition = this.txtcondition.Text;
            string[] arr = condition.Split(',');
            try
            {
                Type modelBase = null;
                Assembly assembly = Assembly.LoadFile(this.txtdllpath.Text);
                foreach (var str in arr)
                {
                    foreach (var item in assembly.GetTypes())
                    {
                        if (item.Name == str) {
                            modelBase = item;
                            break;
                        }
                    }
                }

                var types = assembly.GetTypes();//.Where(t => modelBase == t.BaseType && t != modelBase);
                if (types.Count() > 0)
                {
                    DbHelper.ConnectionString = DbConfig.GetConfig().ConnectionString;
                    DocumentHelper.GenerateDbTableFieldDescribe(types.ToArray(), this.txtcodepath.Text);
                }
              
                MessageBox.Show("根据模型生成数据库表字段说明完成！！！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //清空缓存
            LCache.Instance.Clear();
            button2.Enabled = true;
            button1.Enabled = true;
            button3.Enabled = true;
           
        }
    }
}
